package com.cuitrip.business.user.proxy;

import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;

/**
 * 用户信息代理类
 * Created by ak on 15/9/23.
 */
public class UserInfoProxy {


    private static UserInfoProxy sInstance;

    public static UserInfoProxy getInstance() {
        if (null == sInstance)
            sInstance = new UserInfoProxy();
        return sInstance;
    }

    /**
     * 是否是旅行者
     * @return true 旅行者 false 反之
     */
    public boolean isTraveller(){
        CtUserInfo userInfo = LoginInstance.getInstance().getUserInfo();
        if (userInfo == null || userInfo.isTravel()){
            return true;
        }
        return false;
    }

    public String getSecondInfoLine(String address, String career) {
        return (TextUtils.isEmpty(address) ? ResourceUtil.getString(R.string.user_attribute_city) + " "+ResourceUtil.getString(R.string.value_no_set) : address) + "/"
                + (TextUtils.isEmpty(career) ? ResourceUtil.getString(R.string.user_attribute_work) + " "+ ResourceUtil.getString(R.string.value_no_set) : career);

    }

    public SpannableStringBuilder getCertBuilder(CtUserInfo info) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        int checkedColor = ResourceUtil.getThemeColor();

        if (info.isPhoneValidated()){
            SpannableString phoneSpan = new SpannableString(ResourceUtil.getString(R.string.phone_icon) + " ");
            setColorSpan(phoneSpan, checkedColor);
            spannableStringBuilder.append(phoneSpan);
        }


        if (info.isIdentityValidated()){
            SpannableString idIdentity = new SpannableString(ResourceUtil.getString(R.string.identity_icon) + " ");
            setColorSpan(idIdentity, checkedColor);
            spannableStringBuilder.append(idIdentity);
        }

        if (info.isEmailValidated()){
            SpannableString emailSpan = new SpannableString(ResourceUtil.getString(R.string.mail_full_icon) + " ");
            setColorSpan(emailSpan, checkedColor);
            spannableStringBuilder.append(emailSpan);
        }

        return spannableStringBuilder;
    }

    public SpannableStringBuilder getSocialBindBuilder(CtUserInfo info) {
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        int checkedColor = ResourceUtil.getThemeColor();

        if (info.isWechatValidated()) {
            SpannableString phoneSpan = new SpannableString(ResourceUtil.getString(R.string.wechat_icon) + " ");
            setColorSpan(phoneSpan, checkedColor);
            spannableStringBuilder.append(phoneSpan);
        }

        if(info.isWeiboValidated()){
            SpannableString phoneSpan = new SpannableString(ResourceUtil.getString(R.string.weibo_icon) + " ");
            setColorSpan(phoneSpan, checkedColor);
            spannableStringBuilder.append(phoneSpan);
        }

        if (info.isFacebookValidated()) {
            SpannableString idIdentity = new SpannableString(ResourceUtil.getString(R.string.facebook_icon) + " ");
            setColorSpan(idIdentity, checkedColor);
            spannableStringBuilder.append(idIdentity);
        }

        if (info.isGoogleValidated()) {
            SpannableString emailSpan = new SpannableString(ResourceUtil.getString(R.string.google_plus_icon) + " ");
            setColorSpan(emailSpan, checkedColor);
            spannableStringBuilder.append(emailSpan);
        }


        return spannableStringBuilder;
    }

    public SpannableStringBuilder getFullBindBuilder(CtUserInfo info){
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        spannableStringBuilder.append(getCertBuilder(info));
        if (spannableStringBuilder.length() != 0){
            spannableStringBuilder.append(" ");
        }
        spannableStringBuilder.append(getSocialBindBuilder(info));
        return spannableStringBuilder;
    }


    private void setColorSpan(SpannableString spannableString, int color) {
        spannableString.setSpan(new ForegroundColorSpan(color), 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }


}
